package cn.dankal.flyaudio.personal;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.UserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.MessageCenterEntity;
import cn.dankal.basiclib.pojo.MessageEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.business.pojo.BaseBeanList;
import cn.dankal.business.pojo.NotificationBean;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.MessageCenterAdapter;
import cn.dankal.flyaudio.configs.OrderPagerConfigs;
import cn.dankal.flyaudio.friend.MessageDetailActivity;

public class MessageManagerActivity extends BaseActivity implements View.OnClickListener {

    private View allView;
    private View paymentView;
    private View sendStoreView;
    private View collectionStoreView;
    private RecyclerView orderListView;
    private MultipleStatusView statusView;
    private MessageCenterAdapter adapter;
    private String messageType = "0";

    private SmartRefreshLayout swipe_toload_layout;
    private List<MessageEntity> systemMessage;
    private List<MessageEntity> appMessage;
    private List<MessageEntity> commentMessage;
    private List<MessageEntity> scoreMessage;
    private int num = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_manager;
    }

    @Override
    public void initComponents() {
        addSingleTitleBar("消息中心");

        swipe_toload_layout = findViewById(R.id.swipe_toload_layout);
        statusView = findViewById(R.id.multiple_status_view);
        initView();
        orderListView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MessageCenterAdapter();
        orderListView.setAdapter(adapter);
        swipe_toload_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                num = 1;
                if(messageType.equals("0")){
                    allView.callOnClick();
                }else if(messageType.equals("1")){
                    paymentView.callOnClick();
                }else if(messageType.equals("2")){
                    sendStoreView.callOnClick();
                }else if(messageType.equals("3")){
                    collectionStoreView.callOnClick();
                }
            }
        });
        swipe_toload_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                num++;
                if(messageType.equals("0")){
                    system_remind_list();
                }else if(messageType.equals("1")){
                    apply_remind_list();
                }else if(messageType.equals("2")){
                    comment_remind_list();
                }else if(messageType.equals("3")){
                    score_remind_list();
                }
            }
        });
        adapter.setOnRvItemClickListener(new OnRvItemClickListener<NotificationBean>() {
            @Override
            public void onItemClick(View v, int position, NotificationBean data) {
                Intent intent = new Intent(MessageManagerActivity.this, MessageDetailActivity.class);
                intent.putExtra("message", data);
                intent.putExtra("messageType", adapter.getMessageType());
                startActivity(intent);
            }
        });
//        fetMessage();
        swipe_toload_layout.autoRefresh();
    }

    private void initView() {
        allView = findViewById(R.id.rl_all);
        paymentView = findViewById(R.id.rl_payment);
        sendStoreView = findViewById(R.id.rl_send_store);
        collectionStoreView = findViewById(R.id.rl_collection_store);
        orderListView = findViewById(R.id.swipe_target);
        allView.setOnClickListener(this);
        paymentView.setOnClickListener(this);
        sendStoreView.setOnClickListener(this);
        collectionStoreView.setOnClickListener(this);
    }

    private void showSystemMessage() {
        allView.setSelected(true);
        paymentView.setSelected(false);
        sendStoreView.setSelected(false);
        collectionStoreView.setSelected(false);

        num =1;
        adapter.clearData();
        adapter.setMessageType(messageType);

    }

    private void showApplyMessage() {
        allView.setSelected(false);
        paymentView.setSelected(true);
        sendStoreView.setSelected(false);
        collectionStoreView.setSelected(false);
        num =1;
        adapter.clearData();
        adapter.setMessageType(messageType);
    }

    private void showCommentMessage() {
        allView.setSelected(false);
        paymentView.setSelected(false);
        sendStoreView.setSelected(true);
        collectionStoreView.setSelected(false);
        num =1;
        adapter.clearData();
        adapter.setMessageType(messageType);
    }

    private void showScoresMessage() {
        allView.setSelected(false);
        paymentView.setSelected(false);
        sendStoreView.setSelected(false);
        collectionStoreView.setSelected(true);
        num =1;
        adapter.clearData();
        adapter.setMessageType(messageType);
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.rl_all == id) {
            messageType = "0";
            showSystemMessage();
            system_remind_list();
        } else if (R.id.rl_payment == id) {
            messageType = "1";
            showApplyMessage();
            apply_remind_list();
        } else if (R.id.rl_send_store == id) {
            messageType = "2";
            showCommentMessage();
            comment_remind_list();
        } else if (R.id.rl_collection_store == id) {
            messageType = "3";
            showScoresMessage();
            score_remind_list();
        }
    }

    private void comment_remind_list(){
        UserServiceFactory.comment_remind_list(String.valueOf(num),"10")
                .subscribe(new AbstractSubscriber<BaseBeanList<NotificationBean>>(this) {

                    @Override
                    public void onNext(BaseBeanList<NotificationBean> notificationBeanBaseBeanList) {
                        if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                            swipe_toload_layout.setEnableLoadMore(false);
                        }else{
                            swipe_toload_layout.setEnableLoadMore(true);
                        }
                        if(num==1){
                            if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                                statusView.showEmpty();
                                return;
                            }

                        }
                        statusView.showContent();
                        adapter.addDatas(notificationBeanBaseBeanList.list,num==1);
                    }
                });
    }

    private void score_remind_list(){
        UserServiceFactory.score_remind_list(String.valueOf(num),"10")
                .subscribe(new AbstractSubscriber<BaseBeanList<NotificationBean>>(this) {

                    @Override
                    public void onNext(BaseBeanList<NotificationBean> notificationBeanBaseBeanList) {
                        if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                            swipe_toload_layout.setEnableLoadMore(false);
                        }else{
                            swipe_toload_layout.setEnableLoadMore(true);
                        }
                        if(num==1){
                            if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                                statusView.showEmpty();
                                return;
                            }

                        }
                        statusView.showContent();
                        adapter.addDatas(notificationBeanBaseBeanList.list,num==1);
                    }
                });
    }

    private void apply_remind_list(){
        UserServiceFactory.apply_remind_list(String.valueOf(num),"10")
                .subscribe(new AbstractSubscriber<BaseBeanList<NotificationBean>>(this) {

                    @Override
                    public void onNext(BaseBeanList<NotificationBean> notificationBeanBaseBeanList) {
                        if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                            swipe_toload_layout.setEnableLoadMore(false);
                        }else{
                            swipe_toload_layout.setEnableLoadMore(true);
                        }
                        if(num==1){
                            if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                                statusView.showEmpty();
                                return;
                            }
                        }
                        statusView.showContent();
                        adapter.addDatas(notificationBeanBaseBeanList.list,num==1);
                    }
                });
    }

    private void system_remind_list(){
        UserServiceFactory.system_remind_list(String.valueOf(num),"10")
                .subscribe(new AbstractSubscriber<BaseBeanList<NotificationBean>>(this) {

                    @Override
                    public void onNext(BaseBeanList<NotificationBean> notificationBeanBaseBeanList) {
                        swipe_toload_layout.finishLoadMore();
                        swipe_toload_layout.finishRefresh();
                        if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                            swipe_toload_layout.setEnableLoadMore(false);
                        }else{
                            swipe_toload_layout.setEnableLoadMore(true);
                        }
                        if(num==1){
                            if(notificationBeanBaseBeanList.list== null || notificationBeanBaseBeanList.list.isEmpty()){
                                statusView.showEmpty();
                                return;
                            }
                        }
                        statusView.showContent();
                        adapter.addDatas(notificationBeanBaseBeanList.list,num==1);
                    }
                });
    }


    private void fetMessage() {
        UserServiceFactory.messageCenter().subscribe(new AbstractSubscriber<MessageCenterEntity>(this) {
            @Override
            public void onNext(MessageCenterEntity messageCenterEntity) {
                Log.d("MessageCenter", JSON.toJSONString(messageCenterEntity));
                systemMessage = messageCenterEntity.getSystemMessage();
                appMessage = messageCenterEntity.getAppMessage();
                commentMessage = messageCenterEntity.getCommentMessage();
                scoreMessage = messageCenterEntity.getScoreMessage();
                showSystemMessage();
//                if (systemMessage != null && !systemMessage.isEmpty()) {
//                    adapter.addDatas(systemMessage, true);
//                }
//                if (appMessage != null && !appMessage.isEmpty()) {
//                    adapter.addDatas(appMessage, false);
//                }
//                if (commentMessage != null && !commentMessage.isEmpty()) {
//                    adapter.addDatas(commentMessage, false);
//                }
//                if (scoreMessage != null && !scoreMessage.isEmpty()) {
//                    adapter.addDatas(scoreMessage, false);
//                }
//                if (followMessage != null && !followMessage.isEmpty()) {
//                    adapter.addDatas(followMessage, false);
//                }
//                adapter.sortByTime();
            }
        });
    }
}
